package me.caofeng.handler;

import me.caofeng.enums.Role;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.handler
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-15 16:45
 * @Version: V1.0
 */
public class RoleTypeHandler implements TypeHandler<Role> {
    public void setParameter(PreparedStatement ps, int i, Role parameter, JdbcType jdbcType) throws SQLException {
       ps.setInt(i,parameter.getId());
    }

    public Role getResult(ResultSet rs, String columnName) throws SQLException {
        int id = rs.getInt(columnName);
        return Role.getRole(id);
    }

    public Role getResult(ResultSet rs, int columnIndex) throws SQLException {
        int id  = rs.getInt(columnIndex);
        return Role.getRole(id);
    }

    public Role getResult(CallableStatement cs, int columnIndex) throws SQLException {
        int id = cs.getInt(columnIndex);
        return Role.getRole(id);
    }
}
